<template>
    <div class="add_edit_box">

        <div class="add_edit_top_box">
            <div class="title">{{ workforceId ? '修改' : '新增' }}</div>
            <img src="../../../../assets/imgx/message-bg.png" alt="">
            <span @click="CancelFun">×</span>
        </div>
        <div class="add_edit_button_box">
            <div class="Item">
                <div class="title">班次：</div>
                <div class="input">
                    <input type="text" placeholder="请输入班次" v-model="form.workforceName">
                </div>
            </div>
            <div class="Item">
                <div class="title">时间范围：</div>
                <div class="input">
                    <el-time-select v-model="form.startTime" :picker-options="{
                        start: '00:00',
                        step: '00:30',
                        end: '23:30'
                    }" placeholder="选择时间">
                    </el-time-select>

                    <span style="font-size: 20px;">
                        -
                    </span>
                    <el-time-select v-model="form.endTime" :picker-options="{
                        start: '00:00',
                        step: '00:30',
                        end: '23:30'
                    }" placeholder="选择时间">
                    </el-time-select>
                </div>
            </div>
            <div class="Item">
                <div class="title">关联用户：</div>
                <div class="input">
                    <div class="userBox">
                        <div @click="selectClickFun">
                            请选择
                            <div class="selectIcon">
                                <i class="el-icon-arrow-down" v-if="IconIsbool"></i>
                                <i class="el-icon-arrow-up" v-else></i>
                            </div>
                        </div>

                        <div class="select_mask" v-show="!IconIsbool">
                            <div style="display:flex" v-for="( item, index ) in   GetUser " @click="checkInputFun(item)">
                                <span :key="index" class="beautify-check" style="
                                
                                width: 22px;
                                margin-right: 10px;
                                display: flex;
                                align-items: center;
                                justify-content: center;">
                                    <input class="select_mask_input" style="width: 80%;" type="checkbox"
                                        v-model="item.admin">
                                </span>
                                {{ item.userName }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="Item">
                <div class="SelectItemBox">


                    <div v-for="(  item, index  ) in GetUser" v-if="item.admin" :key="index"><span class="UserName">{{
                        item.userName }}</span> <span @click="checkDelFun(item)">×</span>
                    </div>


                </div>
            </div>
            <div class="Item">
                <button class="btn" @click="btnFun">确定</button>
            </div>
        </div>

    </div>
</template>
<script>
import { getUserListFunApi, editUserNameFunApi, setUserNameFunApi, workForCeConFig } from "@/api/SettingsApi/index"
export default {
    props: ['AddValueEmit'],
    data() {
        return {
            inBool: false,
            IconIsbool: true,
            CheckedIsbool: false,
            UserList: [],
            GetUser: [],
            getUserArr: [],
            workforceId: "",
            form: {
                workforceName: "",
                startTime: "",
                endTime: "",
                workforceXrefList: [],

            }
        }
    },
    created() {
        this.GetUserlist()
    },
    methods: {

        CancelFun() {
            this.$emit('MyCancelFun')
            this.getUserArr = []
            this.GetUser.forEach((v, i) => {
                v.admin = false
            })
        },
        // 获取班次记录列表
        GetUserlist() {
            getUserListFunApi().then(res => {
                if (res.code == 200) {
                    let list = []
                    res.data.map((v, i) => {
                        if (this.UserList[i]) {
                            if (v.userId == this.UserList[i].userId) {
                                v.admin = true
                            }
                        }
                        list.push({ admin: false, ...v })
                    })
                    this.GetUser = list
                    this.getUserArr = list
                } else {
                    this.$emit("errorFun", res)
                }
            })
        },
        // 下拉框功能
        selectClickFun() {
            this.IconIsbool = !this.IconIsbool;
        },
        //添加编辑反选功能
        checkInputFun(item) {
            item.admin = !item.admin

        },
        // 选中删除
        checkDelFun(item) {
            item.admin = false
        },
        // 添加和编辑功能
        btnFun() {
            this.form.workforceXrefList = []
            this.GetUser.forEach((v, i) => {
                if (v.admin == true) {
                    this.form.workforceXrefList.push(v)
                }
            })

            if (this.workforceId) {
                editUserNameFunApi({
                    workforceId: this.workforceId,
                    ...this.form
                }).then(res => {
                    if (res.code == 200) {
                        this.form.workforceName = '';
                        this.form.startTime = '';
                        this.form.endTime = '';
                        this.form.workforceXrefList = [];
                        this.workforceId = "";
                        this.$emit("AddFun")
                    } else {
                        this.$emit("errorFun", res)

                    }
                })
            } else {
                setUserNameFunApi(this.form).then(res => {
                    if (res.code == 200) {
                        this.form.workforceName = '';
                        this.form.startTime = '';
                        this.form.endTime = '';
                        this.form.workforceXrefList = [];
                        this.workforceId = "";
                        this.$emit("AddFun")
                    } else {
                        this.$emit("errorFun", res)

                    }

                })
            }




        }
    },
    // 回显功能
    watch: {
        'AddValueEmit': {
            handler(newVal, oldVal) {
                if (newVal.workforceId) {
                    this.workforceId = newVal.workforceId
                    workForCeConFig({
                        workforceId: newVal.workforceId
                    }).then(res => {
                        this.form = res.data

                        this.getUserArr = res.data.workforceXrefList
                        this.GetUser.forEach((v, i) => {
                            if (this.GetUser[i] != null) {
                                this.getUserArr.forEach((item, index) => {
                                    if (v.userId == item.userId) {
                                        v.admin = true
                                    }
                                })

                            }
                        })
                    })

                } else {

                    this.form.workforceName = '';
                    this.form.startTime = '';
                    this.form.endTime = '';
                    this.workforceId = "";
                    this.GetUserlist()

                }

            },
            deep: true,

        },
        "$store.state.UserList.user": {
            handler(newval, oldval) {
                this.UserList = newval;

            }
        }
    }
}

</script>
<style lang="less" scoped>
.UserName {
    display: inline-block;
    width: 100%;
    text-align: center;
    font-size: 14px !important;
    line-height: 30px !important;
}


.add_edit_box {
    width: 100%;
    height: 100%;

    .add_edit_top_box {
        width: 100%;
        height: 29px;
        position: relative;
        display: flex;

        .title {
            height: 100%;
            text-align: center;
            line-height: 29px;
            color: white;
            font-size: 16px;
        }

        img {
            height: 100%;
            width: 365px;
            position: absolute;
            right: 0;
        }

        span {
            width: 18px;
            height: 18px;
            text-align: right;
            font-size: 36px;
            font-weight: bold;
            color: #0E66B3;
            z-index: 99;
            cursor: pointer;
            line-height: 22px;
            position: absolute;
            right: 15px;
        }
    }

    .add_edit_button_box {
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;

        .Item {
            width: 90%;
            color: white;
            margin-top: 10px;

            .title {
                width: 100%;
                height: 44px;
                line-height: 44px;
            }

            .input {
                width: 100%;
                height: 20px;


                .el-date-editor.el-input {
                    width: 120px;
                    height: 30px;
                    background: #031331;
                    outline: 0;
                    border: 2px solid #09244C;
                    color: #4C6278;
                    display: inline-block;
                }

                input {
                    width: 120px;
                    height: 30px;
                    background: #031331;
                    outline: 0;
                    border: 2px solid #09244C;
                    color: #4C6278;


                }

                .userBox {
                    width: 120px;
                    height: 25px;
                    background: #031331;
                    outline: 0;
                    border: 0;
                    position: relative;
                    line-height: 25px;
                    color: #4C6278;
                    border: 2px solid #09244C;

                    .selectIcon {
                        position: absolute;
                        width: 10%;
                        right: 5px;
                        top: 0px;

                    }

                    .select_mask {
                        position: absolute;
                        top: 25px;
                        width: 100%;
                        height: 100px;
                        overflow: auto;
                        background: #031331;
                        z-index: 99;

                        div {
                            width: 120px;
                            height: 30px;
                            margin-top: 10px;
                        }
                    }
                }


            }

            .SelectItemBox {
                width: 100%;
                height: 80px;
                background: #031331;
                color: white;
                display: flex;
                margin-top: 10px;
                flex-wrap: wrap;
                border: 2px solid #09244C;
                flex-direction: row;
                overflow: auto;

                div {
                    position: relative;
                    width: 105px;
                    margin: 5px;
                    height: 30px;
                    line-height: 30px;
                    // border: 1px solid #3785CE;
                    color: #4C6278;
                    background: #182D55;

                    span {
                        position: absolute;
                        right: 0;
                        color: #3785CE;
                        cursor: pointer;
                        font-size: 25px;
                    }
                }
            }

            .btn {
                width: 136px;
                height: 30px;
                background: #00194B;
                border: 0;
                color: white;
                border: 0.44px solid #26517C;

                margin-left: 130px;
                margin-top: 7px;
            }
        }

    }

}

/deep/.el-input__inner {
    padding: 0px !important;
}

/deep/ .el-input__inner {
    width: 120px;
    height: 30px;
    background: #031331;
    outline: 0;
    border: 2px solid #09244C;
    color: #4C6278;
    display: inline-block;
}

/deep/.el-input__icon {
    line-height: 25px;
    height: 111%;
    display: none;
}
</style>